<?php
namespace app\index\service;
use think\Db;
use app\admin\validate\Increase as IncreaseValidate;
use app\admin\service\IncreaseService as AdmIncService;

class IncreaseService
{
	
	/**
     * 根据工资获取所有险种的基数
     * @param int $wage 工资
     * @return array
     */
    public static function allBase($wage)
    {
    	$list = Db::name('system_insurance_base')->field('insurance,min,max')->select();
		$arr = [];
		foreach ($list as $k => $v) {
			if($wage <= $v['min']){
				$arr[$v['insurance']]['personal'] = $v['min'];
			}elseif($wage > $v['min'] && $wage < $v['max']){
				$arr[$v['insurance']]['personal'] = sprintf("%.2f",$wage);
			}else{
				if($v['max'] > 0){
					$arr[$v['insurance']]['personal'] = $v['max'];
				}else{
					$arr[$v['insurance']]['personal'] = sprintf("%.2f",$wage);
				}
			}
			if($wage < $v['min']){
				$arr[$v['insurance']]['company'] = $v['min'];
			}else{
				$arr[$v['insurance']]['company'] = sprintf("%.2f",$wage);
			}
		}
		return $arr;
    }
	
	
   /**
     * 根据工资获取单一险种的基数
     * @param int $insurance 险种
	 * @param int $wage 工资
     * @return array
     */
    public static function oneBase($insurance,$wage)
    {
    	$base = Db::name('system_insurance_base')->where(['insurance'=>$insurance])->field('min,max')->find();
		if($wage <= $base['min']){
			$personal = $base['min'];
		}elseif($wage > $base['min'] && $wage < $base['max']){
			$personal = sprintf("%.2f",$wage);
		}else{
			if($base['max'] > 0){
				$personal = $base['max'];
			}else{
				$personal = sprintf("%.2f",$wage);
			}
		}
		if($wage < $base['min']){
			$company = $base['min'];
		}else{
			$company = sprintf("%.2f",$wage);
		}		
		return ['company'=>$company,'personal'=>$personal];
    }
	
	
	
	/**
     * 批量增员
     * @param array $arr   表格数据
	 * @param array $contract    公司合同
     * @return array
     */
    public static function import_increase($arr,$contract)
	{
		
		$suss = [];
        $err = [];
		$validate = new IncreaseValidate;
		$point = Db::name('system_service')->where(['is_special'=>2])->value('point');
		foreach($arr as $k=>$v){
			//检查公司合同状态是否正常
			if($contract['status'] != 1){
				$err[] = [
					'name'  => $v[0],
					'idcard' => $v[1],
					'msg' => '账号管理服务关闭，无法增员'
				];
				continue;
			}	
			//检查套餐类型
			if(in_array($contract['setmeal_type'],[2,3])){
				$person_count = Db::name('system_person')->where(['company_id'=>$contract['company_id'],'status'=>1])->count();
				if($person_count >= $contract['people']){
					$err[] = [
						'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '人数套餐已达上限，无法增员'
					];
					continue;
				}
				$deduction = 0;
				$point = 0; 
			}else{
				//检查服务点数
				$deduction = AdmIncService::deduction_check($contract['company_id']);
				if(!$deduction){
					$err[] = [
						'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '服务点不足，无法办理增员'
					];
					continue;
				}
			}				
			//检查姓名		
			$check['name'] = $v[0];
    		if(!$validate->scene('import_name')->check($check)){
    			$err[] = [
					'name'  => $v[0],
					'idcard' => $v[1],
					'msg' => $validate->getError()
				];
				continue;
	       	}
			//检查身份证	
			$check['id_number'] = $v[1];	
    		if(!$validate->scene('import_id')->check($check)){
    			$err[] = [
					'name'  => $v[0],
					'idcard' => $v[1],
					'msg' => $validate->getError()
				];
				continue;
	       	}
			if(!is_idcard($v[1])){
				$err[] = [
					'name'  => $v[0],
					'idcard' => $v[1],
					'msg' => '身份证号格式错误'
				];
				continue;
			}
			$person = Db::name('system_person')->where(['id_number'=>$v[1],'status'=>1])->count();
			if($person){
				$err[] = [
					'name'  => $v[0],
					'idcard' => $v[1],
					'msg' => '该员工已在职'
				];
				continue;
			}
			$count = Db::name('system_increase')->where(['id_number'=>$v[1],'is_deleted'=>0,'status'=>1])->count();
			if($count){
				$err[] = [
					'name'  => $v[0],
					'idcard' => $v[1],
					'msg' => '该员工正在办理增员'
				];
				continue;
			}
			//检查电话号码
			$check['mobile'] = $v[2];	
			if(!$validate->scene('import_mobile')->check($check)){
    			$err[] = [
    				'name'  => $v[0],
					'idcard' => $v[1],
					'msg' => $validate->getError()
				];
				continue;
	       	}
			//检查起始日期
//			if(!$v[3]){
//  			$err[] = [
//  				'name'  => $v[0],
//					'idcard' => $v[1],
//					'msg' => '起始日期不能为空'
//				];
//				continue;
//	       	}else{
//	       		$timestamp = strlen($v[3]) == 5 ? strtotime(excel_time_eval($v[3])) : strtotime($v[3]); 
//	       		if(!is_timestamp($timestamp)){
//	       			$err[] = [
//	    				'name'  => $v[0],
//						'idcard' => $v[1],
//						'msg' => '起始日期格式错误'
//					];
//					continue;
//	       		}
//	       	}
			//检查月发工资
			if(!$v[3]){
				$err[] = [
					'name'  => $v[0],
					'idcard' => $v[1],
					'msg' => '月发工资不能为空'
				];
				continue;
			}
			if(!is_numeric($v[3]) || $v[3] <= 0){
				$err[] = [
					'name'  => $v[0],
					'idcard' => $v[1],
					'msg' => '月发工资格式错误'
				];
				continue;
			}
			//检查托管档案
			if(!$v[4] || $v[4] == '否'){
				$archives = 0;
			}else{
				$archives = 1;
			}
			//检查银行卡号
//			$check['bank_account'] = $v[6];
//			if(!$validate->scene('import_bank_account')->check($check)){
//  			$err[] = [
//  				'name'  => $v[0],
//					'idcard' => $v[1],
//					'msg' => $validate->getError()
//				];
//				continue;
//	       	}
			//检查开户银行
//			$check['bank'] = $v[7];
//			if(!$validate->scene('import_bank')->check($check)){
//  			$err[] = [
//  				'name'  => $v[0],
//					'idcard' => $v[1],
//					'msg' => $validate->getError()
//				];
//				continue;
//	       	}
			//检查银行行号
//			$check['bank_number'] = $v[8];
//			if(!$validate->scene('import_bank_number')->check($check)){
//  			$err[] = [
//  				'name'  => $v[0],
//					'idcard' => $v[1],
//					'msg' => $validate->getError()
//				];
//				continue;
//	       	}
            $start_date = service_accounting_period();
			//检查养老
			if($v[5] != '否' && $v[6]){
				$timestamp = strlen($v[6]) == 5 ? strtotime(excel_time_eval($v[6])) : strtotime($v[6]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '养老起始日期格式错误'
					];
					continue;
	       		}
	       		if(!after_accounting($start_date,date('Y-m-d',$timestamp))){
	       			$err[] = [
	    				'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '养老起始日期超期'
					];
					continue;
	       		}
			}
			if($v[5] != '否' && $v[7]){
				if(!is_numeric($v[7]) || $v[7] <= 0){					
					$err[] = [
						'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '养老社保工资格式错误'
					];
					continue;
				}
			}
			//检查工伤
			if($v[8] != '否' && $v[9]){
				$timestamp = strlen($v[9]) == 5 ? strtotime(excel_time_eval($v[9])) : strtotime($v[9]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '工伤起始日期格式错误'
					];
					continue;
	       		}
	       		if(!after_accounting($start_date,date('Y-m-d',$timestamp))){
	       			$err[] = [
	    				'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '工伤起始日期超期'
					];
					continue;
	       		}
			}
			if($v[8] != '否' && $v[10]){
				if(!is_numeric($v[10]) || $v[10] <= 0){					
					$err[] = [
						'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '工伤社保工资格式错误'
					];
					continue;
				}
			}
			//检查失业
			if($v[11] != '否' && $v[12]){
				$timestamp = strlen($v[12]) == 5 ? strtotime(excel_time_eval($v[12])) : strtotime($v[12]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '失业起始日期格式错误'
					];
					continue;
	       		}
	       		if(!after_accounting($start_date,date('Y-m-d',$timestamp))){
	       			$err[] = [
	    				'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '失业起始日期超期'
					];
					continue;
	       		}
			}
			if($v[11] != '否' && $v[13]){
				if(!is_numeric($v[13]) || $v[13] <= 0){					
					$err[] = [
						'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '失业社保工资格式错误'
					];
					continue;
				}
			}
			//检查医疗
			if($v[14] != '否' && $v[15]){
				$timestamp = strlen($v[15]) == 5 ? strtotime(excel_time_eval($v[15])) : strtotime($v[15]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '医疗起始日期格式错误'
					];
					continue;
	       		}
	       		if(!after_accounting($start_date,date('Y-m-d',$timestamp))){
	       			$err[] = [
	    				'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '医疗起始日期超期'
					];
					continue;
	       		}
			}
			if($v[14] != '否' && $v[16]){
				if(!is_numeric($v[16]) || $v[16] <= 0){					
					$err[] = [
						'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '医疗社保工资格式错误'
					];
					continue;
				}
			}
			//检查公积金
			if($v[17] != '否' && $v[18]){
				$timestamp = strlen($v[18]) == 5 ? strtotime(excel_time_eval($v[18])) : strtotime($v[18]); 
	       		if(!is_timestamp($timestamp)){
	       			$err[] = [
	    				'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '公积金起始日期格式错误'
					];
					continue;
	       		}
	       		if(!after_accounting($start_date,date('Y-m-d',$timestamp))){
	       			$err[] = [
	    				'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '公积金起始日期超期'
					];
					continue;
	       		}
			}
			if($v[17] != '否' && $v[19]){
				if(!is_numeric($v[19]) || $v[19] <= 0){					
					$err[] = [
						'name'  => $v[0],
						'idcard' => $v[1],
						'msg' => '公积金工资格式错误'
					];
					continue;
				}
			}
			$code = number_rule_max(7);
			$inc = [
				'code' => $code,
				'name' => $v[0],
				'id_number' => $v[1],
				'mobile' => $v[2],
				'company_id' => $contract['company_id'],
				'contract_id' => $contract['id'],
				'start_date' => $start_date,
				'wage' => $v[3],
				'archives' => $archives,
				'csr_id' => $contract['csr_id'],
				'source' => 2,
				'deduction' => $deduction,
				'point' => $point
			];
			$inc_id = Db::name('system_increase')->insertGetId($inc); 
			if($inc_id){
				save_number_rule_max(7);
			}	
			//扣除服务点数或次数
			AdmIncService::deduction($inc_id);		
			$insurances = Db::name('system_contract_insurance')->where(['contract_id'=>$contract['id'],'status'=>1])->column('insurance');
			$areas = Db::name('system_contract_insurance')->where(['contract_id'=>$contract['id'],'status'=>1])->column('area','insurance');
	    	if($v[5] != '否' && in_array(1, $insurances)){
	    		$wage = $v[7] ? $v[7] : $v[3];
				if($v[6]){
					$sdate = strlen($v[6]) == 5 ? date('Y-m-d',strtotime(excel_time_eval($v[6]))) : date('Y-m-d',strtotime($v[6])); 
				}else{
					$sdate = $start_date.'-01'; 
				}
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $contract['company_id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 1,
	    			'area' => $areas[1],
	    			'status' => 1,
	    			'wage' => $wage,
	    			'company_base' => self::oneBase(1,$wage)['company'],
	    			'personal_base' => self::oneBase(1,$wage)['personal'],
	    			'start_date' => $sdate,
	    			'csr_id' => $contract['csr_id']
	    		]; 
	    	}else{
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $contract['company_id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 1,
	    			'area' => 0,
	    			'status' => 0,
	    			'wage' => 0,
	    			'company_base' => 0,
	    			'personal_base' => 0,
	    			'start_date' => '',
	    			'csr_id' => $contract['csr_id']
	    		];				
	    	}
			Db::name('system_increase_insurance')->insert($data);
	    	if($v[8] != '否' && in_array(2, $insurances)){
	    		$wage = $v[10] ? $v[10] : $v[3];
				if($v[9]){
					$sdate = strlen($v[9]) == 5 ? date('Y-m-d',strtotime(excel_time_eval($v[9]))) : date('Y-m-d',strtotime($v[9])); 
				}else{
					$sdate = $start_date.'-01'; 
				}
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $contract['company_id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 2,
	    			'area' => $areas[2],
	    			'status' => 1,
	    			'wage' => $wage,
	    			'company_base' => self::oneBase(2,$wage)['company'],
	    			'personal_base' => self::oneBase(2,$wage)['personal'],
	    			'start_date' => $sdate,
	    			'csr_id' => $contract['csr_id']
	    		];				 
	    	}else{
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $contract['company_id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 2,
	    			'area' => 0,
	    			'status' => 0,
	    			'wage' => 0,
	    			'company_base' => 0,
	    			'personal_base' => 0,
	    			'start_date' => '',
	    			'csr_id' => $contract['csr_id']
	    		];				
	    	}
			Db::name('system_increase_insurance')->insert($data);	
			if($v[11] != '否' && in_array(3, $insurances)){
	    		$wage = $v[13] ? $v[13] : $v[3];
				if($v[12]){
					$sdate = strlen($v[12]) == 5 ? date('Y-m-d',strtotime(excel_time_eval($v[12]))) : date('Y-m-d',strtotime($v[12])); 
				}else{
					$sdate = $start_date.'-01';  
				}
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $contract['company_id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 3,
	    			'area' => $areas[3],
	    			'status' => 1,
	    			'wage' => $wage,
	    			'company_base' => self::oneBase(3,$wage)['company'],
	    			'personal_base' => self::oneBase(3,$wage)['personal'],
	    			'start_date' => $sdate,
	    			'csr_id' => $contract['csr_id']
	    		];
	    	}else{
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $contract['company_id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 3,
	    			'area' => 0,
	    			'status' => 0,
	    			'wage' => 0,
	    			'company_base' => 0,
	    			'personal_base' => 0,
	    			'start_date' => '',
	    			'csr_id' => $contract['csr_id']
	    		];				
	    	}
			Db::name('system_increase_insurance')->insert($data);	
	    	if($v[14] != '否' && in_array(4, $insurances)){
	    		$wage = $v[16] ? $v[16] : $v[3];
				if($v[15]){
					$sdate = strlen($v[15]) == 5 ? date('Y-m-d',strtotime(excel_time_eval($v[15]))) : date('Y-m-d',strtotime($v[15])); 
				}else{
					$sdate = $start_date.'-01'; 
				}				
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $contract['company_id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 4,
	    			'area' => $areas[4],
	    			'status' => 1,
	    			'wage' => $wage,
	    			'company_base' => self::oneBase(4,$wage)['company'],
	    			'personal_base' => self::oneBase(4,$wage)['personal'],
	    			'start_date' => $sdate,
	    			'csr_id' => $contract['csr_id']
	    		]; 
	    	}else{
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $contract['company_id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 4,
	    			'area' => 0,
	    			'status' => 0,
	    			'wage' => 0,
	    			'company_base' => 0,
	    			'personal_base' => 0,
	    			'start_date' => '',
	    			'csr_id' => $contract['csr_id']
	    		];				
	    	}
			Db::name('system_increase_insurance')->insert($data);		
			if($v[17] != '否' && in_array(5, $insurances)){
	    		$wage = $v[19] ? $v[19] : $v[3];
				if($v[18]){
					$sdate = strlen($v[18]) == 5 ? date('Y-m-d',strtotime(excel_time_eval($v[18]))) : date('Y-m-d',strtotime($v[18])); 
				}else{
					$sdate = $start_date.'-01';  
				}
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $contract['company_id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 5,
	    			'area' => $areas[5],
	    			'status' => 1,
	    			'wage' => $wage,
	    			'company_base' => self::oneBase(5,$wage)['company'],
	    			'personal_base' => self::oneBase(5,$wage)['personal'],
	    			'start_date' => $sdate,
	    			'csr_id' => $contract['csr_id']
	    		]; 
	    	}else{
	    		$data = [
	    			'increase_id' => $inc_id,
	    			'company_id' => $contract['company_id'],
	    			'contract_id' => $contract['id'],
	    			'insurance' => 5,
	    			'area' => 0,
	    			'status' => 0,
	    			'wage' => 0,
	    			'company_base' => 0,
	    			'personal_base' => 0,
	    			'start_date' => '',
	    			'csr_id' => $contract['csr_id']
	    		];				
	    	}
			Db::name('system_increase_insurance')->insert($data);	
			$suss[] = [
				'name'  => $v[0],
				'idcard' => $v[1],
				'msg' => '增员成功'
			];					    
		}
		return ['suss'=>$suss,'err'=>$err];
	}
	
}